package com.zhangxiaofanfan.hotcode.fourth.third;

/**
 *
 * @date 2024-07-13 03:14:13
 * @author zhangxiaofanfan
 */
public class HotCode083 {
    public static void main(String[] args) {
        HotCode083 hotCode = new HotCode083();
        int[] nums = {2,7,9,3,1};
        System.out.println(hotCode.rob(nums));
    }

    public int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }
        int[] dp = new int[] {nums[0], Math.max(nums[0], nums[1])};
        for (int i = 2; i < nums.length; i++) {
            int max = Math.max(dp[1], dp[0] + nums[i]);
            dp[0] = dp[1];
            dp[1] = max;
        }
        return dp[1];
    }
}
